{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorFlow2教程-不规则向量\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/doit/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import, division, print_function, unicode_literals\n",
    "\n",
    "import math\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据有多种形式。张量也应该如此。 不规则的张量是嵌套可变长度列表的TensorFlow等效项。它们使存储和处理形状不均匀的数据变得容易，包括：\n",
    "\n",
    "- 可变长度功能，例如电影中的一组演员。\n",
    "- 成批的可变长度顺序输入，例如句子或视频剪辑。\n",
    "- 分层输入，例如细分为小节，段落，句子和单词的文本文档。\n",
    "- 结构化输入中的各个字段，例如协议缓冲区。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 不规则张量的作用\n",
    "不规则张量受一百多个TensorFlow操作的支持，其中包括数学操作（如tf.add和tf.reduce_mean），数组操作（如tf.concat和tf.tile），字符串操作op（如 tf.substr）以及许多其他功能："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.RaggedTensor [[6, 4, 7, 4], [], [8, 12, 5], [9], []]>\n",
      "tf.Tensor([2.25              nan 5.33333333 6.                nan], shape=(5,), dtype=float64)\n",
      "<tf.RaggedTensor [[3, 1, 4, 1], [], [5, 9, 2], [6], [], [5, 3]]>\n",
      "<tf.RaggedTensor [[3, 1, 4, 1, 3, 1, 4, 1], [], [5, 9, 2, 5, 9, 2], [6, 6], []]>\n",
      "<tf.RaggedTensor [[b'So', b'lo'], [b'th', b'fo', b'al', b'th', b'fi']]>\n"
     ]
    }
   ],
   "source": [
    "digits = tf.ragged.constant([[3,1,4,1], [], [5,9,2], [6],[]])\n",
    "words = tf.ragged.constant([[\"So\", \"long\"], [\"thanks\", \"for\", \"all\", \"the\", \"fish\"]])\n",
    "print(tf.add(digits, 3))\n",
    "print(tf.reduce_mean(digits, axis=1))\n",
    "print(tf.concat([digits, [[5, 3]]], axis=0))\n",
    "print(tf.tile(digits, [1, 2]))\n",
    "print(tf.strings.substr(words, 0, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
